package grap;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Calendar;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import astar.FindPath;
import data.Label;
import data.Point;
import ui.GameMap;
import util.ReadMap;
import util.WriteMap;

/**
 * @author wangtong
 */
public class ShowMap {
	private GameMap gameMap;
	private JButton btnRead;
	private JButton btnCreat;
	private JButton btnSave;
	private JTextField fileNameRead;
	private JTextField fileNameWrite;
	
	public ShowMap() {
		gameMap = new GameMap();
	}
	
	public void init() {
		initComponen();
		
		initListen();
	}
	
	//showMap有自己的组件
	private void initComponen() {
		gameMap.controlPanel.removeAll();
		
		btnRead = new JButton("read");
		btnRead.setBounds(88, 10, 66, 23);
		gameMap.controlPanel.add(btnRead);
		
		fileNameRead = new JTextField();
		fileNameRead.setBounds(10, 11, 66, 21);
		fileNameRead.setColumns(10);
		fileNameRead.setText("2-12-50-40");
		gameMap.controlPanel.add(fileNameRead);
		
		btnCreat = new JButton("creat");
		btnCreat.setBounds(336, 10, 74, 23);
		gameMap.controlPanel.add(btnCreat);
		
		fileNameWrite = new JTextField();
		fileNameWrite.setBounds(176, 11, 66, 21);
		fileNameWrite.setColumns(10);
		gameMap.controlPanel.add(fileNameWrite);
		
		btnSave = new JButton("save");
		btnSave.setBounds(252, 10, 74, 23);
		gameMap.controlPanel.add(btnSave);
		gameMap.repaint();
	}
	
	private void initListen() {
		btnRead.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String fileName = fileNameRead.getText();
				if(fileName == null || fileName.length() == 0) {
					return;
				}
				ReadMap read = new ReadMap(fileName);
				read.read();
				gameMap.updateGrap(read.getMatrix().length, read.getMatrix()[0].length);
				gameMap.updateLabel(read.getMatrix());
				initLabelListen();
			}
		});
		
		btnSave.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				String fileName =  fileNameRead.getText();				
				if(fileName == null || fileName.length() == 0) {
					Calendar cal = Calendar.getInstance();
					fileName = cal.get(Calendar.MONTH) + 1 + "-" + cal.get(Calendar.DATE) +"-"+ cal.get(Calendar.MINUTE)+"-"+ cal.get(Calendar.SECOND);
				}
				WriteMap out = new WriteMap(fileName, gameMap.grapData);
				out.write();
				JOptionPane.showMessageDialog(null, "success", "Message", JOptionPane.INFORMATION_MESSAGE); 
			}
		});
		
		btnCreat.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				CreateMap creatMap = new CreateMap();
				creatMap.initListen();
			}
		});
	}
	
	//初始化label的监听事件
	private void initLabelListen() {
		gameMap.grapData.setStart(new Point(0,0));
		for(Label[] labels : gameMap.labels) {
			for(Label label : labels) {
				label.addMouseListener(new MouseAdapter() {
					public void mouseClicked(MouseEvent e) {
						listenLabel((Label) e.getSource());
					}
				});
			}
		}
	}
	
	private void listenLabel(Label label){
		//清空之前的start与end标签颜色
		gameMap.setLabelColor(gameMap.grapData.getStart(),new Color(238,238,238));
		gameMap.setLabelColor(gameMap.grapData.getEnd(),new Color(238,238,238));
		
		//清空之前的路径颜色
		updatePathLabel(gameMap.grapData.getPath(),new Color(238,238,238));
		
		Point p = new Point(label.x,label.y);
		if(gameMap.grapData.getEnd() != null){
			gameMap.grapData.setStart(gameMap.grapData.getEnd());
		}
		gameMap.grapData.setEnd(p);
		
		//将start 设为red end设为yellow
		gameMap.setLabelColor(gameMap.grapData.getStart(), Color.red);
		gameMap.setLabelColor(gameMap.grapData.getEnd(), Color.yellow);
		
		//astar begin
		FindPath find = new FindPath(gameMap.grapData);
		List<Point> path = find.getPath();
		path.remove(gameMap.grapData.getStart());
		gameMap.grapData.setPath(path);
		//寻路完成后,设置路径label颜色
		updatePathLabel(path,Color.green);
	}
	
	private void updatePathLabel(List<Point> path, Color c){
		if(path == null || path.size() == 0) {
			return;
		}
		for(Point p : path){
			gameMap.setLabelColor(p, c);
		}
	}
	
	public static void main(String[] args) {
		ShowMap show = new ShowMap();
		show.init();
	}
}
